**************************************************************************************************
*** This do file creates the replication results for 
*** Authors in alphabetical order: Denise Laroze, Eric Neumayer, Thomas Pluemper.
*** Covid-19 does not Stop at Open Borders: Spatial Contagion among Local Authority Districts During England’s First Wave 
*** Social Science & Medicine, Volume 270, February 2021, 113655, https://doi.org/10.1016/j.socscimed.2020.113655. 
							
**************************************************************************************************
**************************************************************************************************
/* Note: You have to change "local DIR" to the directory you copy the original stata files contained 	*/
/* in the zip file and then run the do file. 
/* You must have Stata version 13 or higher installed													*/
**************************************************************************************************
*/

drop _all
clear matrix
clear mata


capture net install outreg2, from(http://fmwww.bc.edu/RePEc/bocode/o) /* checks whether outreg2 is installed 		*/

capture net install spmon, from(http://fmwww.bc.edu/RePEc/bocode/s) /* checks whether spmon is installed 		*/

***********************************************************************************
local DIR = "D:\Research\Health for all database\United Kingdom\"  /*change relative path to the directory where the files are located */
cd "`DIR'"
***********************************************************************************


use "Article for Social Science & Medicine (C19).dta", clear

xtset code_i Week_number 

 
********************************************************
*** Preparation of spatial residuals
********************************************************

**** Pre-lockdown 03/02 to 22/03


* IV regression
reg cases100_w_splag_comm  p_over65_comm p_rural_comm mean_income_2019_comm IMD_10pc_deprived_comm p_ethn_BAME_comm    p_over65_cont p_rural_cont mean_income_2019_cont IMD_10pc_deprived_cont p_ethn_BAME_cont   p_rural  mean_income_2019   IMD_10pc_deprived  p_ethn_BAME_percent  i.Week_number if Week_number<13
capture drop cases100_w_splag_comm_pred
predict cases100_w_splag_comm_pred

capture drop l_w_cases100_i
gen l_w_cases100_i=l.w_cases100_i
label var l_w_cases100_i "=l.w_cases100_i"

capture drop l_cases100_w_splag_comm_pred
gen l_cases100_w_splag_comm_pred=l.cases100_w_splag_comm_pred
label var l_cases100_w_splag_comm_pred "=l.cases100_w_splag_comm_pred"


xtreg w_cases100_i /*
*/ l_w_cases100_i /*
*/ p_rural /*
*/ mean_income_2019   IMD_10pc_deprived /*
*/ p_ethn_BAME_percent /*
*/ l_cases100_w_splag_comm_pred  i.Week_number if Week_number<13, vce(bootstrap, reps(500) seed(1))


capture drop predictions 
capture drop resid_13_weeks_before
predict predictions if e(sample)
gen resid_13_weeks_before=w_cases100_i-predictions



**** During lockdown 23/03 to 10/05


* IV regression
xtreg cases100_w_splag_comm  p_over65_comm p_rural_comm mean_income_2019_comm IMD_10pc_deprived_comm p_ethn_BAME_comm   p_over65_cont p_rural_cont mean_income_2019_cont IMD_10pc_deprived_cont p_ethn_BAME_cont   p_rural  mean_income_2019   IMD_10pc_deprived  p_ethn_BAME_percent  i.Week_number if Week_number>=13 & Week_number<20
capture drop cases100_w_splag_comm_pred
predict cases100_w_splag_comm_pred

capture drop l_w_cases100_i
gen l_w_cases100_i=l.w_cases100_i
label var l_w_cases100_i "=l.w_cases100_i"

capture drop l_cases100_w_splag_comm_pred
gen l_cases100_w_splag_comm_pred=l.cases100_w_splag_comm_pred
label var l_cases100_w_splag_comm_pred "=l.cases100_w_splag_comm_pred"


xtreg w_cases100_i /*
*/ l_w_cases100_i /*
*/ p_rural /*
*/ mean_income_2019   IMD_10pc_deprived /*
*/ p_ethn_BAME_percent /*
*/ l_cases100_w_splag_comm_pred  i.Week_number if Week_number>=13 & Week_number<20, vce(bootstrap, reps(500) seed(1))


capture drop predictions 
capture drop resid_13_weeks_during
predict predictions if e(sample)
gen resid_13_weeks_during=w_cases100_i-predictions



**** Post-lockdown ease 11/05 to 25/06


* IV regression
xtreg cases100_w_splag_comm  p_over65_comm p_rural_comm mean_income_2019_comm IMD_10pc_deprived_comm p_ethn_BAME_comm   p_over65_cont p_rural_cont mean_income_2019_cont IMD_10pc_deprived_cont p_ethn_BAME_cont    p_rural  mean_income_2019  IMD_10pc_deprived  p_ethn_BAME_percent  i.Week_number if Week_number>=20
capture drop cases100_w_splag_comm_pred
predict cases100_w_splag_comm_pred

capture drop l_w_cases100_i
gen l_w_cases100_i=l.w_cases100_i
label var l_w_cases100_i "=l.w_cases100_i"

capture drop l_cases100_w_splag_comm_pred
gen l_cases100_w_splag_comm_pred=l.cases100_w_splag_comm_pred
label var l_cases100_w_splag_comm_pred "=l.cases100_w_splag_comm_pred"


xtreg w_cases100_i /*
*/ l_w_cases100_i /*
*/ p_rural /*
*/ mean_income_2019   IMD_10pc_deprived /*
*/ p_ethn_BAME_percent /*
*/ l_cases100_w_splag_comm_pred  i.Week_number if Week_number>=20, vce(bootstrap, reps(500) seed(1))

capture drop predictions 
capture dorp resid_13_weeks_after
predict predictions if e(sample)
gen resid_13_weeks_after=w_cases100_i-predictions



**** Save spatial residuals

preserve
keep code_i Week_number resid_*
ren Week_number week
ren code_i code_k
save "spatial residuals", replace
restore





 
********************************************************
*** Spatial error generation
********************************************************


use "Article for Social Science & Medicine (C19) spatial source data.dta", clear

set more off, perm
 
mmerge code_k week using "spatial residuals"
 
sum commuters_total

capture drop comm_tot_fraction
gen comm_tot_fraction=commuters_total/r(max) 
label var  comm_tot_fraction "=commuters_total/r(max)"


capture drop not_contig
gen not_contig=1-contiguous
label var not_contig "=1-contiguous"

capture drop pop_100
gen pop_100=population_2018_k/100000
label var pop_100 "=population_2018_k/100000"

capture drop resid_13_weeks_before_tot
gen resid_13_weeks_before_tot=resid_13_weeks_before*pop_100
label var resid_13_weeks_before_tot "=resid_13_weeks_before*pop_100"

capture drop resid_13_weeks_during_tot
gen resid_13_weeks_during_tot=resid_13_weeks_during*pop_100
label var resid_13_weeks_during_tot "=resid_13_weeks_during*pop_100"

capture drop resid_13_weeks_after_tot
gen resid_13_weeks_after_tot=resid_13_weeks_after*pop_100
label var resid_13_weeks_after_tot "=resid_13_weeks_after*pop_100"



* population
spmon  pop_100, w(contiguous) i(code_i) k(code_k) sename(pop_100_tot_cont) filename (pop_100_tot_cont) norow time(week)
spmon  pop_100, w(comm_tot_fraction) i(code_i) k(code_k) sename(pop_100_tot_comm) filename (pop_100_tot_comm) norow time(week)


* before week 13
spmon  resid_13_weeks_before_tot, w(contiguous) i(code_i) k(code_k) sename(resid_13_weeks_before_cont_tot) filename (resid_13_weeks_before_cont_tot) norow time(week)
spmon  resid_13_weeks_before_tot, w(comm_tot_fraction) i(code_i) k(code_k) sename(resid_13_weeks_before_comm_tot) filename (resid_13_weeks_before_comm_tot) norow time(week)


* week 13 onwards & before week 20
spmon  resid_13_weeks_during_tot, w(contiguous) i(code_i) k(code_k) sename(resid_13_weeks_during_cont_tot) filename (resid_13_weeks_during_cont_tot) norow time(week)
spmon  resid_13_weeks_during_tot, w(comm_tot_fraction) i(code_i) k(code_k) sename(resid_13_weeks_during_comm_tot) filename (resid_13_weeks_during_comm_tot) norow time(week)

* week 20 onwards
spmon  resid_13_weeks_after_tot, w(contiguous) i(code_i) k(code_k) sename(resid_13_weeks_after_cont_tot) filename (resid_13_weeks_after_cont_tot) norow time(week)
spmon  resid_13_weeks_after_tot, w(comm_tot_fraction) i(code_i) k(code_k) sename(resid_13_weeks_after_comm_tot) filename (resid_13_weeks_after_cont_tot) norow time(week)



* spatial errors per 100,000
gen resid_13_weeks_before_cont=resid_13_weeks_before_cont_tot/pop_100_tot_cont
gen resid_13_weeks_during_cont=resid_13_weeks_during_cont_tot/pop_100_tot_cont
gen resid_13_weeks_after_cont=resid_13_weeks_after_cont_tot/pop_100_tot_cont

gen resid_13_weeks_before_comm=resid_13_weeks_before_comm_tot/pop_100_tot_comm
gen resid_13_weeks_during_comm=resid_13_weeks_during_comm_tot/pop_100_tot_comm
gen resid_13_weeks_after_comm=resid_13_weeks_after_comm_tot/pop_100_tot_comm

collapse resid_13_*cont resid_13_*comm, by(code_i week)

decode code_i, gen(code_i_string)

ren week Week_number

save "spatial errors.dta", replace




 
********************************************************
*** Estimation results
********************************************************

use "Article for Social Science & Medicine (C19).dta", clear

decode code_i, gen(code_i_string)

merge 1:1 code_i_string Week_number using "spatial errors.dta"
drop if _merge==2

drop _merge
 

 
xtset code_i Week_number 



**** Pre-lockdown 03/02 to 22/03


* IV regression
xtreg cases100_w_splag_comm  p_over65_comm p_rural_comm mean_income_2019_comm IMD_10pc_deprived_comm  p_ethn_BAME_comm   p_over65_cont p_rural_cont mean_income_2019_cont IMD_10pc_deprived_cont p_ethn_BAME_cont   p_rural  mean_income_2019   IMD_10pc_deprived  p_ethn_BAME_percent  i.Week_number if Week_number<13
capture drop cases100_w_splag_comm_pred
predict cases100_w_splag_comm_pred

capture drop l_w_cases100_i
gen l_w_cases100_i=l.w_cases100_i
label var l_w_cases100_i "=l.w_cases100_i"

capture drop l_cases100_w_splag_comm_pred
gen l_cases100_w_splag_comm_pred=l.cases100_w_splag_comm_pred
label var l_cases100_w_splag_comm_pred "=l.cases100_w_splag_comm_pred"


xtreg w_cases100_i /*
*/ l_w_cases100_i /*
*/ p_rural /*
*/ mean_income_2019   IMD_10pc_deprived /*
*/ p_ethn_BAME_percent /*
*/ l_cases100_w_splag_comm_pred  resid_13_weeks_before_cont  i.Week_number if Week_number<13, vce(bootstrap, reps(500) seed(1))
outreg2 using table1.xls, excel replace  


capture drop SL_coef_before
gen SL_coef_before=_b[l_cases100_w_splag_comm_pred]
capture drop SL_se_before
gen SL_se_before=_se[l_cases100_w_splag_comm_pred]


capture drop TL_coef_before
gen TL_coef_before=_b[l_w_cases100_i]
capture drop TL_se_before
gen TL_se_before=_se[l_w_cases100_i]





**** During lockdown 23/03 to 10/05


* IV regression
xtreg cases100_w_splag_comm  p_over65_comm p_rural_comm mean_income_2019_comm IMD_10pc_deprived_comm  p_ethn_BAME_comm   p_over65_cont p_rural_cont mean_income_2019_cont IMD_10pc_deprived_cont p_ethn_BAME_cont   p_rural  mean_income_2019   IMD_10pc_deprived  p_ethn_BAME_percent  i.Week_number if Week_number>=13 & Week_number<20
capture drop cases100_w_splag_comm_pred
predict cases100_w_splag_comm_pred

capture drop l_w_cases100_i
gen l_w_cases100_i=l.w_cases100_i
label var l_w_cases100_i "=l.w_cases100_i"

capture drop l_cases100_w_splag_comm_pred
gen l_cases100_w_splag_comm_pred=l.cases100_w_splag_comm_pred
label var l_cases100_w_splag_comm_pred "=l.cases100_w_splag_comm_pred"


xtreg w_cases100_i /*
*/ l_w_cases100_i /*
*/ p_rural /*
*/ mean_income_2019   IMD_10pc_deprived /*
*/ p_ethn_BAME_percent /*
*/ l_cases100_w_splag_comm_pred resid_13_weeks_during_cont i.Week_number if Week_number>=13 & Week_number<20, vce(bootstrap, reps(500) seed(1))
outreg2 using table1.xls, excel append  


capture drop SL_coef_during
gen SL_coef_during=_b[l_cases100_w_splag_comm_pred]
capture drop SL_se_during
gen SL_se_during=_se[l_cases100_w_splag_comm_pred]


capture drop TL_coef_during
gen TL_coef_during=_b[l_w_cases100_i]
capture drop TL_se_during
gen TL_se_during=_se[l_w_cases100_i]



capture drop SL_before_during_test
gen SL_before_during_test=(SL_coef_before-SL_coef_during)/(sqrt(SL_se_before^2+SL_se_during^2))
di SL_before_during_test


capture drop TL_before_during_test
gen TL_before_during_test=(TL_coef_before-TL_coef_during)/(sqrt(TL_se_before^2+TL_se_during^2))
di TL_before_during_test




**** Post-lockdown ease 11/05 to 25/06


* IV regression
xtreg cases100_w_splag_comm  p_over65_comm p_rural_comm mean_income_2019_comm IMD_10pc_deprived_comm p_ethn_BAME_comm   p_over65_cont p_rural_cont mean_income_2019_cont IMD_10pc_deprived_cont p_ethn_BAME_cont   p_rural  mean_income_2019   IMD_10pc_deprived  p_ethn_BAME_percent  i.Week_number if Week_number>=20
capture drop cases100_w_splag_comm_pred
predict cases100_w_splag_comm_pred

capture drop l_w_cases100_i
gen l_w_cases100_i=l.w_cases100_i
label var l_w_cases100_i "=l.w_cases100_i"

capture drop l_cases100_w_splag_comm_pred
gen l_cases100_w_splag_comm_pred=l.cases100_w_splag_comm_pred
label var l_cases100_w_splag_comm_pred "=l.cases100_w_splag_comm_pred"


xtreg w_cases100_i /*
*/ l_w_cases100_i /*
*/ p_rural /*
*/ mean_income_2019   IMD_10pc_deprived /*
*/ p_ethn_BAME_percent /*
*/ l_cases100_w_splag_comm_pred resid_13_weeks_after_cont i.Week_number if Week_number>=20, vce(bootstrap, reps(500) seed(1))
outreg2 using table1.xls, excel append  


capture drop SL_coef_after
gen SL_coef_after=_b[l_cases100_w_splag_comm_pred]
capture drop SL_se_after
gen SL_se_after=_se[l_cases100_w_splag_comm_pred]

capture drop SL_before_during_test
gen SL_before_during_test=(SL_coef_before-SL_coef_during)/(sqrt(SL_se_before^2+SL_se_during^2))
di SL_before_during_test

capture drop SL_before_after_test
gen SL_before_after_test=(SL_coef_before-SL_coef_after)/(sqrt(SL_se_before^2+SL_se_after^2))
di SL_before_after_test

capture drop SL_during_after_test
gen SL_during_after_test=(SL_coef_during-SL_coef_after)/(sqrt(SL_se_during^2+SL_se_after^2))
di SL_during_after_test

